<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container">
      <el-input class="filter-item" style="width: 200px; margin-left: 0" size="mini" placeholder="请输入要搜索的内容标题" v-model="form.searchTitle"/>
      <el-select class="filter-item" style="width: 160px" v-model="form.searchType" size="mini" @change="handlePageInit">
        <el-option v-for="item in pageData.searchTypeList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-select class="filter-item" style="width: 160px" v-model="form.searchStatus" size="mini" @change="handlePageInit">
        <el-option v-for="item in pageData.searchStatusList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handlePageInit">搜索</el-button>
      <el-button type="primary" icon="el-icon-share" size="mini" @click="handleOpenSavePage(2)">推送广告</el-button>
      <el-button type="primary" icon="el-icon-share" size="mini" @click="handleOpenSavePage(1)">推送内容</el-button>
      <el-button type="primary" icon="el-icon-share" size="mini" @click="handleOpenSavePage(3)">推送专题</el-button>
      <el-button type="success" icon="el-icon-share" size="mini" @click="handleBatchReleaseData" :disabled="form.selection.length < 1">批量发布</el-button>
      <el-button type="danger" icon="el-icon-share" size="mini" @click="handleBatchDeleteData" :disabled="form.selection.length < 1">批量删除</el-button>
    </div>
    <!-- 页面数据列表 -->
    <div class="data-table">
      <el-table :data="responseData.tableList"
                border style="width: 100%;"
                v-loading="pageData.loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :header-cell-style="{ background: '#545C64', color: '#ffffff' }"
                @selection-change="handleTableSelection">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column fixed="left" prop="targetId" label="数据ID" width="340" align="center"></el-table-column>
        <el-table-column prop="articleTitle" label="内容标题" width="360" align="center"></el-table-column>
        <el-table-column prop="sequenceNum" label="展示顺序" width="80" align="center"></el-table-column>
        <el-table-column prop="targetType" label="数据类型" width="80" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.targetType === 1"><el-tag type="warning">内容</el-tag></span>
            <span v-if="scope.row.targetType === 2"><el-tag type="success">广告</el-tag></span>
            <span v-if="scope.row.targetType === 3"><el-tag type="primary">专题</el-tag></span>
            <span v-if="scope.row.targetType === 4"><el-tag type="info">课程</el-tag></span>
            <span v-if="scope.row.targetType === 5"><el-tag type="danger">报名</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="displayStatus" label="审核状态" width="100" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.displayStatus === 0"><el-tag type="warning">下架</el-tag></span>
            <span v-if="scope.row.displayStatus === 1"><el-tag type="success">发布</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="displayCreateTime" label="创建时间" width="180" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" width="260">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleOpenSettingPage(scope)">设置顺序</el-button>
            <el-button v-if="scope.row.displayStatus === 0" size="mini" type="success" @click="handleUpdateDataStatus(scope,1)">发布</el-button>
            <el-button v-if="scope.row.displayStatus === 1" size="mini" type="warning" @click="handleUpdateDataStatus(scope,0)">下架</el-button>
            <el-button size="mini" type="danger" @click="handleDeleteData(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页器-->
    <div style="margin-top: 20px">
      <div class="block" style="width: 100%; text-align: center">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="form.pageNum"
          :page-size="form.pageSize"
          :total="form.total"
          :page-sizes="[12, 50, 100, 200]"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange">
        </el-pagination>
      </div>
    </div>
    <!--插入广告面板-->
    <div style="width: 60%; margin: auto">
      <el-dialog :title="pageData.formPanelTitle" :visible.sync="pageData.formPanelStatus" size="small"
                 :before-close="handleCloseSaveBannerPage">
        <el-form ref="form" :model="form" label-width="120px">
          <el-form-item label="数据ID">
            <el-select v-model="form.targetId" filterable remote reserve-keyword placeholder="请输入预设广告名称" :remote-method="handleRemoteData">
              <el-option v-for="item in responseData.remoteSearchList" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="数据类型">
            <el-radio-group v-model="form.targetType" size="medium" disabled>
              <el-radio :label="1">内容</el-radio>
              <el-radio :label="2">广告</el-radio>
              <el-radio :label="3">专题</el-radio>
              <el-radio :label="4">课程</el-radio>
              <el-radio :label="5">报名</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="发布状态">
            <el-radio v-model="form.displayStatus" :label="0">下架</el-radio>
            <el-radio v-model="form.displayStatus" :label="1">发布</el-radio>
          </el-form-item>
          <el-form-item label="展示顺序">
            <el-input v-model="form.sequenceNum"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSaveButtonClick">{{pageData.formPanelTitle}}</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
    <!--修改顺序面板-->
    <div style="width: 60%; margin: auto">
      <el-dialog title="展示设置" :visible.sync="pageData.formSettingStatus" size="small"
                 :before-close="handleCloseSettingPage">
        <el-form ref="form" :model="form" label-width="120px">
          <el-form-item label="设置数据">
            <el-input v-model="form.targetTitle"></el-input>
          </el-form-item>
          <el-form-item label="设置顺序">
            <el-input v-model="form.sequenceNum"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleUpdateButtonClick">更新设置</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsHomeDataList,loadCmsDataList,saveCmsHomeData,batchReleaseCmsHomeData,batchDeleteCmsHomeData,updateCmsHomeData } from "@/api/content";
  export default {
    name: "home",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data (){
      return {
        pageData:{
          searchTypeList: [
            {label: "请选择", value: -1},
            {label: "广告", value: 2},
            {label: "内容", value: 1},
            {label: "专题", value: 3}
          ],// 类型筛选预设选项
          searchStatusList: [
            {label: "发布状态", value: -1},
            {label: "下架", value: 0},
            {label: "发布", value: 1}
          ],// 发布状态预设选项
          loading: false,// 登录LOAD图
          formPanelStatus: false,// 广告新增面板隐藏域
          formPanelTitle: "",
          formSettingStatus: false,// 设置面板隐藏域
        },// 页面配置数据
        form:{
          searchTitle: "",
          searchType: "",
          searchStatus: "",
          pageNum: 1,// 表单--页码
          pageSize: 12,// 表单--页面
          total: 0,// 表单--总量
          selection: [],// 多选的内容
          displayId: "",
          targetId: "",
          targetTitle: "",
          targetType: 0,
          sequenceNum: 0,
          displayStatus: 0
        },// 页面表单
        formRules: {
        },// 表单验证
        responseData: {
          tableList: [],// 页面表格数据
          remoteSearchList: [],// 远程搜索结果集
        },// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods: {
      // 事件函数
      handlePageInit(){
        this.form.pageNum = 1;
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsHomeDataList();// 读取路由角色列表
      },// 页面初始化事件
      handleCurrentChange(val){
        this.form.pageNum = val;// 更换页码
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsHomeDataList();// 读取路由角色列表
      },// 页码切换事件
      handleSizeChange(val){
        this.form.pageSize = val;// 更换页面大小
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsHomeDataList();// 读取路由角色列表
      },// 页面size切换事件
      handleTableSelection(val){
        let list = [];// 初始化多选数组
        for (let i=0; i<val.length; i++){
          list.push(val[i].displayId);
        }// 遍历多选的列表，将ID拼入数组中
        this.form.selection = list;// 进行多选值的切换
      },// 表格多选事件
      handleOpenSavePage(type){
        this.form.displayId = "";
        this.form.targetId = "";
        this.form.targetTitle = "";
        this.form.targetType = type;
        this.form.sequenceNum = 0;
        this.form.displayStatus = 0;
        this.responseData.remoteSearchList = [];
        this.pageData.formPanelStatus = true;
        switch (type) {
          case 1:
            this.pageData.formPanelTitle = "推送内容";
            break;
          case 2:
            this.pageData.formPanelTitle = "推送广告";
            break;
          case 3:
            this.pageData.formPanelTitle = "推送专题";
            break;
          case 4:
            this.pageData.formPanelTitle = "推送课程";
            break;
          case 5:
            this.pageData.formPanelTitle = "推送报名";
            break;
        }
      },// 打开广告新增面板
      handleCloseSaveBannerPage(){
        this.$confirm("确认关闭？").then(response => {
          this.form.displayId = "";
          this.form.targetId = "";
          this.form.targetTitle = "";
          this.form.targetType = 0;
          this.form.sequenceNum = 0;
          this.form.displayStatus = 0;
          this.responseData.remoteSearchList = [];
          this.pageData.formPanelStatus = false;
          this.pageData.formPanelTitle = "";
        }).catch(err => {
          console.log(err);
        });
      },// 关闭广告新增面板
      handleRemoteData(query){
        if (query !== ""){
          let param = {
            searchTitle: query,
            searchType: this.form.targetType
          };
          this.loadCmsDataList(param);
        }
      },// 新增隐藏域远程搜索数据事件
      handleSaveButtonClick(){
        if (PermissionsToDetectFormUser(getPermission(), "460001")){
          this.$confirm('是否保存当前数据到首页?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.saveCmsHomeData();
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 新增隐藏域保存按钮点击事件
      handleBatchReleaseData(){
        if (PermissionsToDetectFormUser(getPermission(), "460005")){
          this.$confirm("是否发布当前所选数据?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              displayId: this.form.selection.toString(),
              displayStatus: 1
            };
            this.batchReleaseCmsHomeData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 批量发布按钮点击事件
      handleBatchDeleteData(){
        if (PermissionsToDetectFormUser(getPermission(), "460002")){
          this.$confirm("是否删除当前所选数据?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              displayId: this.form.selection.toString(),
            };
            this.batchDeleteCmsHomeData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 批量删除按钮点击事件
      handleUpdateDataStatus(scope,status){
        if (PermissionsToDetectFormUser(getPermission(), "460005")){
          this.$confirm("是否更改当前所选数据的发布状态?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              displayId: scope.row.displayId,
              displayStatus: status
            };
            this.batchReleaseCmsHomeData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 更改表格内容发布状态
      handleDeleteData(scope){
        if (PermissionsToDetectFormUser(getPermission(), "460002")){
          this.$confirm("是否删除当前所选数据?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              displayId: scope.row.displayId,
            };
            this.batchDeleteCmsHomeData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 表格删除按钮点击事件
      handleOpenSettingPage(scope){
        this.form.displayId = scope.row.displayId;
        this.form.targetTitle = scope.row.articleTitle;
        this.form.sequenceNum = scope.row.sequenceNum;
        this.pageData.formSettingStatus = true;
      },// 打开设置面板
      handleCloseSettingPage(){
        this.$confirm("确认关闭？").then(response => {
          this.form.displayId = "";
          this.form.targetId = "";
          this.form.targetTitle = "";
          this.form.targetType = 0;
          this.form.sequenceNum = 0;
          this.form.displayStatus = 0;
          this.responseData.remoteSearchList = [];
          this.pageData.formSettingStatus = false;
          this.pageData.formPanelTitle = "";
        }).catch(err => {
          console.log(err);
        });
      },// 关闭设置面板
      handleUpdateButtonClick(){
        if (PermissionsToDetectFormUser(getPermission(), "460003")){
          this.$confirm('是否保存当前数据到首页?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.updateCmsHomeData();
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 修改按钮点击事件
      // 接口
      loadCmsHomeDataList(){
        loadCmsHomeDataList(this.form).then(response => {
          if (response.code === "200"){
            this.pageData.loading = false;
            this.responseData.tableList = response.data.list;
            this.form.total = response.data.total;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取首页列表数据
      loadCmsDataList(param){
        loadCmsDataList(param).then(response => {
          if (response.code === "200"){
            this.responseData.remoteSearchList = response.data;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取首页管理页面搜索的数据列表（根据字符）
      saveCmsHomeData(){
        saveCmsHomeData(this.form).then(response => {
          if (response.code === "200"){
            this.form.displayId = "";
            this.form.targetId = "";
            this.form.targetTitle = "";
            this.form.targetType = 0;
            this.form.sequenceNum = 0;
            this.form.displayStatus = 0;
            this.responseData.remoteSearchList = [];
            this.pageData.formPanelStatus = false;
            this.pageData.formPanelTitle = "";
            this.$message.success("保存成功");
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      },// 保存首页推送数据
      batchReleaseCmsHomeData(param){
        batchReleaseCmsHomeData(param).then(response => {
          if (response.code === "200"){
            this.$message.success(param.displayStatus === 1 ? "发布成功" : "下架成功");
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      },// 批量发布首页数据
      batchDeleteCmsHomeData(param){
        batchDeleteCmsHomeData(param).then(response => {
          if (response.code === "200"){
            this.$message.warning("删除成功");
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      },// 批量发布首页数据
      updateCmsHomeData(){
        updateCmsHomeData(this.form).then(response => {
          if (response.code === "200"){
            this.form.displayId = "";
            this.form.targetId = "";
            this.form.targetTitle = "";
            this.form.targetType = 0;
            this.form.sequenceNum = 0;
            this.form.displayStatus = 0;
            this.responseData.remoteSearchList = [];
            this.pageData.formSettingStatus = false;
            this.pageData.formPanelTitle = "";
            this.$message.success("保存成功");
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      }// 修改首页内容数据
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function(){
      let _this = this;
      document.onkeydown = function(e) {
        if (!_this.pageData.loading) {
          let key = window.event.keyCode;
          if (key === 13) {
            _this.handlePageInit(); //此方法是当按下enter键后要做的动作。
          }
        }
      };
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  }
</script>

<style scoped>

</style>
